home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1414 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.6 KB  |  43 lines

  1. Path: locutus.rchland.ibm.com!usenet
  2. From: pstaite@vnet.ibm.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Random numbers
  5. Date: 10 Jan 1996 21:36:25 GMT
  6. Organization: IBM OS/2 Device Driver Development  Rochester, MN
  7. Message-ID: <4d1bgp$qn6@locutus.rchland.ibm.com>
  8. References: <4cul2d$gi@news.ran.es>
  9. Reply-To: pstaite@vnet.ibm.com
  10. NNTP-Posting-Host: warpone.rchland.ibm.com
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <4cul2d$gi@news.ran.es>, mper@ran.es writes:
  14. >How many "random" numbers can be created using the rand() function without repitition. 
  15. >I am using this routine for a physics projects which simulates alpha scattering (Rutherford
  16. >Scattering), and I do about 1 million iterations through the main loop, creating 1 million random numbers.
  17. >Obviously, if its just repeating the numbers, the whole point of the simulation is gone.
  18.  
  19. Can't say for sure, try this:
  20.  
  21. #include<iostream.h>
  22. #include<limits.h>
  23.  
  24. int main() {
  25.     cout << RAND_MAX << endl;
  26.     return 0; }
  27.  
  28. And it'll tell you what the max return is from rand().  However, a 
  29. really poor rand() may exhibit cyclic behavior with a much shorter 
  30. period than RAND_MAX.  That is, if RAND_MAX is 32767 (as I bet it is ;-)
  31. rand() may not be capable of generating all 32768 possible values, 
  32. worse, it may have cycles.  Try passing the output of rand() through an 
  33. FFT some time, very interesting... :-)
  34.  
  35. See Knuth's "Art of Computer Programming" Vol II or "Numerical Recipies 
  36. in C" or some other decent algorithms book for a good pseudo random 
  37. number generator.
  38.  
  39.  
  40. Phil Staite, team OS/2
  41. internet: pstaite@vnet.ibm.com  internal: pstaite@rchland
  42.  
  43.